package db

import (
	"context"
	"github.com/gookit/color"
	"github.com/redis/go-redis/v9"
	"log"
	"strconv"
)

//var dsn = "root:root@tcp(192.168.8.20:3307)/1Panel?charset=utf8mb4&parseTime=True&loc=Local"

func InitRedis(config DataBase) *redis.Client {
	atoi, err := strconv.Atoi(config.DataBase)
	if err != nil {
		atoi = 0
	}
	return CreatCon(config.Username, config.Password, config.Host, atoi)
}
func CreatCon(username, password, Addr string, db int) *redis.Client {
	redisClient := redis.NewClient(&redis.Options{
		Username: username,
		Addr:     Addr,
		Password: password,
		DB:       db,
	})
	str, err := redisClient.Ping(context.Background()).Result()
	if err != nil {
		color.Redln(err)
		panic(err)
	}
	log.Println("redis:", str)
	return redisClient
}
